package cn.bfay.lion.rest.service.impl;

import cn.bfay.lion.rest.client.rsp.ProductInfoRsp;
import cn.bfay.lion.rest.entity.UserProduct;
import cn.bfay.lion.rest.manager.JingdongManager;
import cn.bfay.lion.rest.mapper.UserProductMapper;
import cn.bfay.lion.rest.service.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * AbstractService.
 *
 * @author wangjiannan
 * @since 2019/11/6
 */
@Slf4j
@Component
public abstract class AbstractService extends BaseService {
    @Resource
    private JingdongManager jingdongManager;
    @Resource
    private UserProductMapper userProductMapper;

    protected void watchProduct(String openid, String skuid) {
        UserProduct userProduct = new UserProduct();
        userProduct.setOpenid(openid);
        userProduct.setSkuid(skuid);
        userProduct.setMark((byte) 1);
        ProductInfoRsp productInfoRsp = jingdongManager.getProductInfoBySkuid(skuid);
        userProduct.setNoticePrice(Math.floor(productInfoRsp.getNormalPrice() * 0.45));

        UserProduct oldUserProduct = userProductMapper.selectOne(new QueryWrapper<UserProduct>().lambda()
            .eq(UserProduct::getOpenid, openid)
            .eq(UserProduct::getSkuid, skuid));
        if (oldUserProduct == null) {
            userProductMapper.insert(userProduct);
        } else {
            userProduct.setId(oldUserProduct.getId());
            userProductMapper.updateById(userProduct);
        }
    }
}
